/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package pe.edu.pucp.sistemasInf.view;

import java.util.ArrayList;
import javax.swing.JFrame;
import javax.swing.JInternalFrame;
import javax.swing.JOptionPane;
import javax.swing.tree.DefaultMutableTreeNode;
import javax.swing.tree.DefaultTreeModel;
import pe.edu.pucp.sistemasInf.bean.Cliente;
import pe.edu.pucp.sistemasInf.controller.Service;

/**
 *
 * @author alulab11
 */
public class AgregarClienteJefeWindow extends javax.swing.JInternalFrame {

    String cadRUC;
    String cadPhone;
    Cliente clienteSelected;
    ArrayList<Cliente> clientes=new ArrayList<Cliente>();
    
    public AgregarClienteJefeWindow() {
        initComponents();
        loadClientTree();
    }

    
    public void loadClientTree()
    {
        //Carga desde base de datos, los elementos primarios del arbol(department)
        DefaultMutableTreeNode root=new DefaultMutableTreeNode("Empleados");
        DefaultTreeModel model=new DefaultTreeModel(root);
        
        ArrayList<Cliente> listaClientesPadre=new Service().queryAllClientes();
        int indexClientePadre=0;
        if (listaClientesPadre!=null)
        {
            for (Cliente n:listaClientesPadre)
            {
                DefaultMutableTreeNode node=new DefaultMutableTreeNode(n.getRazon_Social() +"-"+n.getRUC());
                model.insertNodeInto(node, root,indexClientePadre );

                ArrayList<Cliente> listaClientesHijo=new Service().queryClientesHijos(n.getCod_Cliente());
                int indexClienteHijo=0;
                if (listaClientesHijo!=null)
                {
                    for (Cliente m:listaClientesHijo)
                    {
                        DefaultMutableTreeNode nodeEmployee=new DefaultMutableTreeNode
                                                            (m.getRazon_Social()+"-"+m.getRUC());
                        model.insertNodeInto(nodeEmployee, node, indexClienteHijo);
                        indexClienteHijo++;
                    }
                }
                indexClientePadre++;
            }
        }
        treeCliente.setModel(model);
    }
    
    
  
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        txtDireccion1 = new javax.swing.JTextField();
        txtRubro = new javax.swing.JTextField();
        txtTelefono = new javax.swing.JTextField();
        jLabel7 = new javax.swing.JLabel();
        jLabel9 = new javax.swing.JLabel();
        txtRUC = new javax.swing.JTextField();
        txtDireccion2 = new javax.swing.JTextField();
        txtRazonSocial = new javax.swing.JTextField();
        jLabel11 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel1 = new javax.swing.JLabel();
        txtCodigoCliente = new javax.swing.JTextField();
        jLabel6 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jScrollPane1 = new javax.swing.JScrollPane();
        treeCliente = new javax.swing.JTree();
        btnModifyOrSave = new javax.swing.JButton();
        btnAddEmployee = new javax.swing.JButton();
        jLabel8 = new javax.swing.JLabel();
        jCheckBox1 = new javax.swing.JCheckBox();
        btnAddEmployee2 = new javax.swing.JButton();
        btnAddEmployee3 = new javax.swing.JButton();
        btnAddEmployee4 = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jLabel12 = new javax.swing.JLabel();
        jLabel13 = new javax.swing.JLabel();
        txtAnexo = new javax.swing.JTextField();
        jLabel14 = new javax.swing.JLabel();
        jLabel15 = new javax.swing.JLabel();
        jLabel16 = new javax.swing.JLabel();
        txtNombreContacto = new javax.swing.JTextField();
        txtCuenta = new javax.swing.JTextField();
        txtTelefonoContacto = new javax.swing.JTextField();

        setClosable(true);
        addInternalFrameListener(new javax.swing.event.InternalFrameListener() {
            public void internalFrameActivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosed(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameClosing(javax.swing.event.InternalFrameEvent evt) {
                formInternalFrameClosing(evt);
            }
            public void internalFrameDeactivated(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameDeiconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameIconified(javax.swing.event.InternalFrameEvent evt) {
            }
            public void internalFrameOpened(javax.swing.event.InternalFrameEvent evt) {
            }
        });

        txtDireccion1.setEditable(false);
        txtDireccion1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                txtDireccion1ActionPerformed(evt);
            }
        });

        txtRubro.setEditable(false);

        txtTelefono.setEditable(false);

        jLabel7.setText("Dirección 1:");

        jLabel9.setText("Dirección 2:");

        txtRUC.setEditable(false);
        txtRUC.addKeyListener(new java.awt.event.KeyAdapter() {
            public void keyTyped(java.awt.event.KeyEvent evt) {
                txtRUCKeyTyped(evt);
            }
            public void keyPressed(java.awt.event.KeyEvent evt) {
                txtRUCKeyPressed(evt);
            }
            public void keyReleased(java.awt.event.KeyEvent evt) {
                txtRUCKeyReleased(evt);
            }
        });

        txtDireccion2.setEditable(false);

        txtRazonSocial.setEditable(false);

        jLabel11.setText("Teléfono:");

        jLabel3.setText("RUC:");

        jLabel1.setText("Código Cliente:");

        txtCodigoCliente.setEditable(false);
        txtCodigoCliente.setEnabled(false);

        jLabel6.setText("Rubro:");

        jLabel2.setText("Razón Social:");

        treeCliente.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                treeClienteMouseClicked(evt);
            }
        });
        jScrollPane1.setViewportView(treeCliente);

        btnModifyOrSave.setText("Modificar Cliente");
        btnModifyOrSave.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnModifyOrSaveActionPerformed(evt);
            }
        });

        btnAddEmployee.setText("Agregar Cliente");
        btnAddEmployee.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddEmployeeActionPerformed(evt);
            }
        });

        jLabel8.setText("Status");

        jCheckBox1.setSelected(true);
        jCheckBox1.setText("Activado");
        jCheckBox1.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jCheckBox1ActionPerformed(evt);
            }
        });

        btnAddEmployee2.setText("Filtrar Cliente");
        btnAddEmployee2.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddEmployee2ActionPerformed(evt);
            }
        });

        btnAddEmployee3.setText("Cerrar");
        btnAddEmployee3.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddEmployee3ActionPerformed(evt);
            }
        });

        btnAddEmployee4.setText("Eliminar");
        btnAddEmployee4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                btnAddEmployee4ActionPerformed(evt);
            }
        });

        jLabel10.setText("Empresa:");

        jLabel12.setText("TCobra");

        jLabel13.setText("Cuenta:");

        txtAnexo.setEditable(false);

        jLabel14.setText("Tlf.Contacto:");

        jLabel15.setText("AnexoContacto:");

        jLabel16.setText("Nombre Contacto:");

        txtNombreContacto.setEditable(false);

        txtCuenta.setEditable(false);

        txtTelefonoContacto.setEditable(false);

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addGap(28, 28, 28)
                .addComponent(jScrollPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 188, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addGap(35, 35, 35)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel10)
                                .addGap(0, 0, Short.MAX_VALUE))
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel1)
                                    .addComponent(jLabel2, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addComponent(jLabel3))
                                .addGap(31, 31, 31)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtRUC)
                                    .addComponent(txtRazonSocial)
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                            .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                                .addGroup(javax.swing.GroupLayout.Alignment.LEADING, layout.createSequentialGroup()
                                                    .addComponent(txtCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, 102, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                    .addGap(46, 46, 46)
                                                    .addComponent(jLabel8)
                                                    .addGap(30, 30, 30)
                                                    .addComponent(jCheckBox1))
                                                .addComponent(jLabel12, javax.swing.GroupLayout.Alignment.LEADING, javax.swing.GroupLayout.PREFERRED_SIZE, 134, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addComponent(txtDireccion1, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                                                .addComponent(txtCuenta, javax.swing.GroupLayout.DEFAULT_SIZE, 310, Short.MAX_VALUE)
                                                .addComponent(txtDireccion2))
                                            .addGroup(layout.createSequentialGroup()
                                                .addComponent(txtRubro, javax.swing.GroupLayout.PREFERRED_SIZE, 98, javax.swing.GroupLayout.PREFERRED_SIZE)
                                                .addGap(38, 38, 38)
                                                .addComponent(jLabel11)
                                                .addGap(18, 18, 18)
                                                .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 112, javax.swing.GroupLayout.PREFERRED_SIZE)))
                                        .addGap(0, 0, Short.MAX_VALUE)))))
                        .addGap(44, 44, 44))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING, false)
                            .addComponent(jLabel6)
                            .addComponent(jLabel13)
                            .addComponent(jLabel7)
                            .addComponent(jLabel9)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(jLabel15)
                                    .addComponent(jLabel14))
                                .addGap(27, 27, 27)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addComponent(txtAnexo)
                                    .addComponent(txtTelefonoContacto)))
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(jLabel16)
                                .addGap(18, 18, 18)
                                .addComponent(txtNombreContacto, javax.swing.GroupLayout.PREFERRED_SIZE, 309, javax.swing.GroupLayout.PREFERRED_SIZE)))
                        .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))))
            .addGroup(layout.createSequentialGroup()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGap(68, 68, 68)
                        .addComponent(btnAddEmployee))
                    .addGroup(layout.createSequentialGroup()
                        .addGap(136, 136, 136)
                        .addComponent(btnAddEmployee2)
                        .addGap(18, 18, 18)
                        .addComponent(btnModifyOrSave)
                        .addGap(18, 18, 18)
                        .addComponent(btnAddEmployee4, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(18, 18, 18)
                        .addComponent(btnAddEmployee3, javax.swing.GroupLayout.PREFERRED_SIZE, 107, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addComponent(jScrollPane1, javax.swing.GroupLayout.DEFAULT_SIZE, 361, Short.MAX_VALUE)
                        .addGap(19, 19, 19))
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel10)
                            .addComponent(jLabel12))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel1)
                            .addComponent(txtCodigoCliente, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel8)
                            .addComponent(jCheckBox1))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtRazonSocial, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel2))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtRUC, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel3))
                        .addGap(13, 13, 13)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(txtRubro, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jLabel6)
                            .addComponent(jLabel11)
                            .addComponent(txtTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel7)
                            .addComponent(txtDireccion1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel9)
                            .addComponent(txtDireccion2, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel13)
                            .addComponent(txtCuenta, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel16)
                            .addComponent(txtNombreContacto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(12, 12, 12)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel15)
                            .addComponent(txtAnexo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addGap(18, 18, 18)
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                            .addComponent(jLabel14)
                            .addComponent(txtTelefonoContacto, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, 45, Short.MAX_VALUE)))
                .addComponent(btnAddEmployee)
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(btnModifyOrSave)
                    .addComponent(btnAddEmployee2)
                    .addComponent(btnAddEmployee3)
                    .addComponent(btnAddEmployee4))
                .addGap(25, 25, 25))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void txtDireccion1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_txtDireccion1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_txtDireccion1ActionPerformed

    private void formInternalFrameClosing(javax.swing.event.InternalFrameEvent evt) {//GEN-FIRST:event_formInternalFrameClosing
        // TODO add your handling code here:
    }//GEN-LAST:event_formInternalFrameClosing

    private void btnAddEmployee2ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddEmployee2ActionPerformed
        // TODO add your handling code here:
        FiltrarClienteWindow f= new FiltrarClienteWindow(new JFrame(),true);
        f.setVisible(true);
    }//GEN-LAST:event_btnAddEmployee2ActionPerformed

    private void btnAddEmployee3ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddEmployee3ActionPerformed
        // TODO add your handling code here:
        this.dispose();
    }//GEN-LAST:event_btnAddEmployee3ActionPerformed

    private void btnAddEmployeeActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddEmployeeActionPerformed
        
        AgregarCliente ac=new AgregarCliente(new JFrame(),true,clienteSelected,this);
        ac.setVisible(true);
    }//GEN-LAST:event_btnAddEmployeeActionPerformed

    private void btnAddEmployee4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnAddEmployee4ActionPerformed
        // TODO add your handling code here:
        EliminarCliente ec=new EliminarCliente(new JFrame(),true);
        ec.setLocation(500, 250);
        ec.setVisible(true);
    }//GEN-LAST:event_btnAddEmployee4ActionPerformed

    private void jCheckBox1ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jCheckBox1ActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_jCheckBox1ActionPerformed
    int selectedid;
    private void btnModifyOrSaveActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_btnModifyOrSaveActionPerformed
        // TODO add your handling code here:
       
       if (btnModifyOrSave.getText().equalsIgnoreCase("Modificar")) 
       {
            btnModifyOrSave.setText("Guardar Cambios");
            txtTelefono.setEditable(true);
            txtRUC.setEditable(true);
            txtRazonSocial.setEditable(true);
            txtCodigoCliente.setEditable(true);
            txtNombreContacto.setEditable(true);
            txtAnexo.setEditable(true);
            txtDireccion1.setEditable(true);
            txtDireccion2.setEditable(true);
            txtCuenta.setEditable(true);
            txtRubro.setEditable(true);
            txtRazonSocial.setEditable(true);
            
       }
       else
       {
           if(!txtCodigoCliente.getText().matches("[1-9]{1,7}") || 
            txtRazonSocial.getText().isEmpty() ||
             !txtRUC.getText().matches("[0-9]{11}") || 
              txtRubro.getText().isEmpty() ||
              !txtTelefono.getText().matches("[0-9]{3}+[-]+[0-9]{4}") ||
              txtDireccion1.getText().isEmpty() ||
              txtDireccion2.getText().isEmpty() ||
              !txtCuenta.getText().matches("[0-9]{3}+[-]+[0-9]{7}+[-]+[0-9]{1}+[-]+[0-9]{2}") ||
              txtNombreContacto.getText().isEmpty() ||
              !txtTelefonoContacto.getText().matches("[0-9]{3}+[-]+[0-9]{4}") ||
              !txtAnexo.getText().matches("[0-9]{1,3}")){
                if(!txtCodigoCliente.getText().matches("[1-9]{1,7}")){
                    JOptionPane.showMessageDialog(this, "Codigo mal ingresado");}
                if(txtRazonSocial.getText().isEmpty()){
                    JOptionPane.showMessageDialog(this, "Razon Social mal ingresada");}
                if(!txtRUC.getText().matches("[0-9]{11}") ){
                    JOptionPane.showMessageDialog(this, "RUC mal ingresado");}
                if(txtRubro.getText().isEmpty()){
                    JOptionPane.showMessageDialog(this, "Rubro mal ingresado");}
                if(!txtTelefono.getText().matches("[0-9]{3}+[-]+[0-9]{4}")){
                    JOptionPane.showMessageDialog(this, "Telefono mal ingresado");}
                if(txtDireccion1.getText().isEmpty()){
                    JOptionPane.showMessageDialog(this, " Direccion 1 mal ingresado");}
                if(txtDireccion2.getText().isEmpty()){
                    JOptionPane.showMessageDialog(this, " Direccion 2 mal ingresado");}
                if(!txtCuenta.getText().matches("[0-9]{3}+[-]+[0-9]{7}+[-]+[0-9]{1}+[-]+[0-9]{2}")){
                    JOptionPane.showMessageDialog(this, " Cuenta mal ingresada");}
                if(txtNombreContacto.getText().isEmpty()){
                    JOptionPane.showMessageDialog(this, " Nombre de Contacto mal ingresado");}
                if(!txtTelefonoContacto.getText().matches("[0-9]{3}+[-]+[0-9]{4}")){
                    JOptionPane.showMessageDialog(this, "Telefono de Contacto mal ingresado");}
                if(!txtAnexo.getText().matches("[0-9]{1,3}")){
                    JOptionPane.showMessageDialog(this, "Anexo mal ingresado");}
            }else{
                Cliente c=new Cliente();
                c.setTelefono(txtTelefono.getText());
                c.setRUC(txtRUC.getText());
                c.setRazon_Social(txtRazonSocial.getText());
                c.setCod_Cliente(Integer.parseInt(txtCodigoCliente.getText()));
                c.setNombre_contacto(txtNombreContacto.getText());
                c.setAnexo_contacto(txtAnexo.getText());
                c.setDireccion2(txtDireccion1.getText());
                c.setDireccion2(txtDireccion2.getText());
                c.setCuenta(txtCuenta.getText());
                c.setRubro(txtRubro.getText());
                String flag;
                if(jCheckBox1.isSelected())  flag="A";
                else flag="D";
                c.setEstado(flag);
                new Service().updateCliente(c);
           }
       }
       
       
    }//GEN-LAST:event_btnModifyOrSaveActionPerformed

    private void treeClienteMouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_treeClienteMouseClicked
        
        if (treeCliente.getSelectionPath()!=null)
        {
            if (treeCliente.getSelectionPath().getPathCount()>=2)
            {   
                String[] val=treeCliente.getSelectionPath().getLastPathComponent().toString().split("-");
                Cliente c=new Service().queryClienteByRUC(val[1]);
                colocarDataCliente(c);
            }
        }
    }//GEN-LAST:event_treeClienteMouseClicked

    private void txtRUCKeyReleased(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRUCKeyReleased
        //       if (txtRUC.getText().length()>11)
        //           txtRUC.setText(cadRUC);
    }//GEN-LAST:event_txtRUCKeyReleased

    private void txtRUCKeyPressed(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRUCKeyPressed
        //       if (txtRUC.getText().length()<=11)
        //            cadRUC=txtRUC.getText();
    }//GEN-LAST:event_txtRUCKeyPressed

    private void txtRUCKeyTyped(java.awt.event.KeyEvent evt) {//GEN-FIRST:event_txtRUCKeyTyped
        //        if (txtRUC.getText().length()>11)
        //           txtRUC.setText(cadRUC);
    }//GEN-LAST:event_txtRUCKeyTyped

    public void colocarDataCliente(Cliente c)
    {
        clienteSelected=c;
        txtAnexo.setText(c.getAnexo_contacto());
        txtCodigoCliente.setText(c.getCod_Cliente()+"");
        txtCuenta.setText(c.getCuenta());
        txtDireccion1.setText(c.getDireccion1());
        txtDireccion2.setText(c.getDireccion2());
        txtNombreContacto.setText(c.getNombre_contacto());
        txtRUC.setText(c.getRUC());
        txtRazonSocial.setText(c.getRazon_Social());
        txtRubro.setText(c.getRubro());
        txtTelefono.setText(c.getTelefono());
        txtTelefonoContacto.setText(c.getTlf_contacto());
    }
    
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton btnAddEmployee;
    private javax.swing.JButton btnAddEmployee2;
    private javax.swing.JButton btnAddEmployee3;
    private javax.swing.JButton btnAddEmployee4;
    private javax.swing.JButton btnModifyOrSave;
    private javax.swing.JCheckBox jCheckBox1;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel12;
    private javax.swing.JLabel jLabel13;
    private javax.swing.JLabel jLabel14;
    private javax.swing.JLabel jLabel15;
    private javax.swing.JLabel jLabel16;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JTree treeCliente;
    private javax.swing.JTextField txtAnexo;
    private javax.swing.JTextField txtCodigoCliente;
    private javax.swing.JTextField txtCuenta;
    private javax.swing.JTextField txtDireccion1;
    private javax.swing.JTextField txtDireccion2;
    private javax.swing.JTextField txtNombreContacto;
    private javax.swing.JTextField txtRUC;
    private javax.swing.JTextField txtRazonSocial;
    private javax.swing.JTextField txtRubro;
    private javax.swing.JTextField txtTelefono;
    private javax.swing.JTextField txtTelefonoContacto;
    // End of variables declaration//GEN-END:variables
}
